using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data;
using Crosland.Framework.DataHelper;
using Crosland.Framework.Base;
using Crosland.Offline.Core;
using Crosland.Offline.Data.Helper;

namespace Crosland.Offline.Data.Dao
{
    public class OpcionDao : DaoBase
    {
        public OpcionDao(string strConn) : base(strConn)
        {
        }

        OpcionHelper helper = new OpcionHelper();

        public int Save(Opcion entity)
        {
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpSave);

            dbProvider.AddInParameter(command, helper.IdOpcion, DbType.Int32, entity.ID);
            dbProvider.AddInParameter(command, helper.IdPadre, DbType.Int32, entity.IdPadre);
            dbProvider.AddInParameter(command, helper.IndEstado, DbType.String, entity.IndEstado);
            dbProvider.AddInParameter(command, helper.DesDescripcion, DbType.String, entity.DesDescripcion);
            dbProvider.AddInParameter(command, helper.DesNombre, DbType.String, entity.DesNombre);
            dbProvider.AddInParameter(command, helper.NroOrden, DbType.Int32, entity.NroOrden);
            dbProvider.AddInParameter(command, helper.NomFormulario, DbType.String, entity.NomFormulario);
            dbProvider.AddInParameter(command, helper.NroNivel, DbType.Int32, entity.NroNivel);

            return (int)dbProvider.ExecuteScalar(command);
        }

        public void Update(Opcion entity)
        {
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpUpdate);

            dbProvider.AddInParameter(command, helper.IdOpcion, DbType.Int32, entity.ID);
            dbProvider.AddInParameter(command, helper.IdPadre, DbType.Int32, entity.IdPadre);
            dbProvider.AddInParameter(command, helper.IndEstado, DbType.String, entity.IndEstado);
            dbProvider.AddInParameter(command, helper.DesDescripcion, DbType.String, entity.DesDescripcion);
            dbProvider.AddInParameter(command, helper.DesNombre, DbType.String, entity.DesNombre);
            dbProvider.AddInParameter(command, helper.NroOrden, DbType.Int32, entity.NroOrden);
            dbProvider.AddInParameter(command, helper.NomFormulario, DbType.String, entity.NomFormulario);
            dbProvider.AddInParameter(command, helper.NroNivel, DbType.Int32, entity.NroNivel);

            dbProvider.ExecuteNonQuery(command);
        }

        public void Delete(System.Int32 id)
        {
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpDelete);

            dbProvider.AddInParameter(command, helper.IdOpcion, DbType.Int32, id);

            dbProvider.ExecuteNonQuery(command);
        }

        public Opcion GetById(System.Int32 id)
        {
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpGetById);

            dbProvider.AddInParameter(command, helper.IdOpcion, DbType.Int32, id);
            Opcion entity = null;

            using (IDataReader dr = dbProvider.ExecuteReader(command))
            {
                if (dr.Read())
                {
                    entity = helper.Create(dr);
                }
            }

            return entity;
        }

        public List<Opcion> List()
        {
            List<Opcion> entitys = new List<Opcion>();
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpList);

            using (IDataReader dr = dbProvider.ExecuteReader(command))
            {
                while (dr.Read())
                {
                    entitys.Add(helper.Create(dr));
                }
            }

            return entitys;
        }

        public List<Opcion> GetByCriteria()
        {
            List<Opcion> entitys = new List<Opcion>();
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpGetByCriteria);

            using (IDataReader dr = dbProvider.ExecuteReader(command))
            {
                while (dr.Read())
                {
                    entitys.Add(helper.Create(dr));
                }
            }

            return entitys;
        }

        public List<Opcion> GetByCriteria(int idGrupo)
        {
            List<Opcion> entitys = new List<Opcion>();
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpGetByCriteria);
            dbProvider.AddInParameter(command, helper.IdRol, DbType.Int32, idGrupo);

            using (IDataReader dr = dbProvider.ExecuteReader(command))
            {
                while (dr.Read())
                {
                    entitys.Add(helper.Create(dr));
                }
            }

            return entitys;
        }


        public List<Opcion> ObtenerPorUsuario(string idUsuario, int idPadre)
        {
            List<Opcion> entitys = new List<Opcion>();
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpObtenerPorUsuarioOpcion);
            dbProvider.AddInParameter(command, helper.IdUsuario, DbType.String, idUsuario);
            dbProvider.AddInParameter(command, helper.IdPadre, DbType.Int32, idPadre);

            using (IDataReader dr = dbProvider.ExecuteReader(command))
            {
                while (dr.Read())
                {
                    entitys.Add(helper.Create(dr));
                }
            }

            return entitys;
        }

        public int GetNroSubNodos(int? idPadre)
        {
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpObtenerNroSubNodos);
            dbProvider.AddInParameter(command, helper.IdPadre, DbType.Int32, idPadre);

            return (int)dbProvider.ExecuteScalar(command);
        }
    }
}
